<?php

namespace app\jun\controller;

use think\controller;
use app\api\model\Postdatadb;

class Base extends \think\Controller {
	protected $ys_config = [ 
			'geteway' => 'https://tailongji.dixintong.com/erp/api/',
			'partnerId' => 25,
			'siteId' => 117,
			'appId' => 'utec',
			'appKey' => 'yserp',
			'appSecret' => 'duXtRelcYy2sJeqrLpmNvbQO0O0OO0O0O' 
	];
	protected $username = 'jun_admin';
	protected $password = '4k3xNmKO';
	protected $member_id = 193;
	protected $site_url = 'https://tailongjipc.dixintong.com/';
	public $request = array ();
	public function _initialize() {
		header ( "Access-Control-Allow-Origin: *" );
		// header('Content-Type:application/json; charset=utf-8');
		define('SITE_ID', 117);
		define('PARTNER_ID', 25);
		define('MEMBER_ID', 193);
		$request = request ();
		$this->request = $request->param ();
		// 捕获请求的数据
		$postdatadb = new Postdatadb ();
		$postdatadb->param ['post_json'] = $this->request;
		$postdatadb->param ['post_url'] = $request->url ();
		$postdatadb->ajaxSave ();
	}
	public function index() {
		exit ( '欢迎使用' );
	}
	// 成功返回值
	public function successReturn($data) {
		$return = $data;
		if (! isset ( $return ['resultMsg'] )) {
			$return ['returnMsg'] = 'success';
		}
		$return ['isSuccess'] = true;
		$this->ajaxReturn ( $return );
	}
    public function junErrorReturn($data) {
        $return = $data;
        $return ['isSuccess'] = false;
        $this->ajaxReturn ( $return );
    }
	// 失败返回值
	public function errorReturn($msg) {
		$return ['isSuccess'] = false;
		$return ['returnMsg'] = $msg;
		$this->ajaxReturn ( $return );
	}
	public function sendInfo($string_uri, $post) {
		$post ['config'] = $this->ys_config;
		$json_data = json_encode ( $post );
		$ch = curl_init ();
		curl_setopt ( $ch, CURLOPT_URL, $this->ys_config ['geteway'] . $string_uri );
		curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
		curl_setopt ( $ch, CURLOPT_POSTFIELDS, $json_data );
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
		curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
		curl_setopt ( $ch, CURLOPT_HTTPHEADER, array (
				"Content-type: application/json;charset='utf-8'" 
		) );
		$result = curl_exec ( $ch );
		curl_close ( $ch );
		return json_decode ( $result, true );
	}
	protected function ajaxReturn($return, $json_option = 0) {
		header ( 'Content-Type:application/json; charset=utf-8' );
		exit ( json_encode ( $return, $json_option ) );
	}
	protected function chkToken() {
		if (! isset ( $this->request ['token'] )) {
			$return ['success'] = false;
			$return ['desc'] = 'token_expired';
			$this->ajaxReturn ( $return );
		}
		$post ['token'] = $this->request ['token'];
		$data = $this->sendInfo ( 'chkToken', $post );
		if ($data ['code'] != 1) {
			$return ['success'] = false;
			$return ['desc'] = 'token_expired';
			$this->ajaxReturn ( $return );
		}
	}
	protected function thirdSendInfo($string_uri, $post) {
		$ch = curl_init ();
		curl_setopt ( $ch, CURLOPT_URL, $this->third_geteway . $string_uri );
		curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
		curl_setopt ( $ch, CURLOPT_POSTFIELDS, http_build_query ( $post ) );
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
		curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
		$result = curl_exec ( $ch );
		curl_close ( $ch );
		dump ( $result );
	}
	public function thirdToken() {
		$time = time ();
		$post_data ['timestamp'] = date ( 'YmdHis', $time );
		$post_data ['username'] = $this->third_name;
		$post_data ['password'] = $this->third_password;
		$post_data ['sign'] = strtolower ( md5 ( $this->third_name . $this->third_password . $post_data ['timestamp'] . $this->third_password ) );
		$return = $this->thirdSendInfo ( 'gyssc/token', $post_data );
		return $return ['result'] ['access_token'];
	}
	public function getInfo() {
		$post_data ['token'] = $this->thirdToken ();
		$return = $this->thirdSendInfo ( 'gyssc/platform/info', $post_data );
		dump ( $return );
	}
	public function getCategory() {
		$post_data ['token'] = $this->thirdToken ();
		$post_data ['platformCode'] = 'ZF00003';
		$return = $this->thirdSendInfo ( 'gyssc/platform/catalogs', $post_data );
		return json_encode ( $return );
	}
	public function getBrand() {
		$post_data ['token'] = $this->thirdToken ();
		$post_data ['platformCode'] = 'ZF00003';
		$return = $this->thirdSendInfo ( 'gyssc/platform/brands', $post_data );
		dump ( $return );
	}
}